/**
 * 将指定格式的 字典定义，返回为一个包含value, 标识，描述的对象
 * value:[标识，描述]
 * 用法：
    export const TASK_STATUS= generateDicObj({
       0: ['unPub', '未发布','未发布描述'],
       1: ['pubbed', '已发布','已发布描述']
    }，[1, 0])
    返回的TASK_STATUS 结构为
    {
        0: '未发布', // 返回描述值
        1: '已发布', // 返回描述值
        unPub: '0', // 返回标识
        pubbed: '1', // 返回标识
        _define: {unpub: '0', pubbed: '1'}, // 返回标识定义对象
        _tag: { 0: unpub, 1:pubbed}, // 返回对应标识对象
        _label: {0: '未发布', 1: '已发布'}  // 返回label对象
        _labels:[{key:'1',label:'已发布'}，{key:'0',label:'未发布'}] // 返回label 数组， 由参数keysOrder指定key的顺序，否则按object默认暂时
        _desc: {0: '未发布描述', 1: '已发布描述'}  // 返回描述对象
    }

    TASK_STATUS[0] >> 未发布
    TASK_STATUS[unPub] >> 0
    TASK_STATUS[_label] >> {0: '未发布', 1: '已发布'}
    TASK_STATUS[_labels] >>[{key:'1',label:'已发布'}，{key:'0',label:'未发布'}]
    TASK_STATUS[_desc] >> {0: '未发布描述', 1: '已发布描述'}
*/
function generateDicObj(obj, keysOrder) {
  const keyMap = new Map()
  const defineObj = {}
  const labelObj = {}
  const flagObj = {}
  const descObj = {}
  const keys = []
  for (const key in obj) {
    const itemVal = obj[key]
    keys.push(key)
    if (Array.isArray(itemVal) && itemVal.length > 0 && itemVal.length <= 3) {
      itemVal.forEach((item, idx) => {
        if (idx === 0) {
          keyMap.set(item, key)
          defineObj[item] = key
          flagObj[key] = item
        } else if (idx === 1) {
          keyMap.set(key, item)
          labelObj[key] = item
        } else if (idx === 2) {
          descObj[key] = item
        }
      })
    } else {
      throw Error('dict define invalid')
    }
  }

  const dictObj = {}
  for (const [key, value] of keyMap) {
    dictObj[key] = value
  }
  dictObj._define = defineObj
  dictObj._tag = flagObj
  dictObj._keys = keys
  if (Object.keys(labelObj).length > 0) {
    dictObj._label = labelObj
  }
  if (Object.keys(descObj).length > 0) {
    dictObj._desc = descObj
  }
  dictObj._labels = keys.map(key => {
    return {
      key,
      label: labelObj[key],
    }
  })
  if (keysOrder) {
    dictObj._labels.sort((a, b) => {
      const aOrder = keysOrder.findIndex(key => String(key) === a.key) // 类型可能会不一样
      const bOrder = keysOrder.findIndex(key => String(key) === b.key)
      if (aOrder !== undefined && bOrder !== undefined) {
        if (aOrder > bOrder) {
          return 1
        } else if (aOrder < bOrder) {
          return -1
        }
      }
      return 0
    })
  }
  return Object.freeze(dictObj)
}

// 文件预览类型
export const FILE_PREVIEW_TYPE = generateDicObj({
  0: ['img', '图片'],
  1: ['doc', '文档'],
  2: ['video', '视频'],
})

// 状态
export const STATE = generateDicObj({
  0: ['stop', '失效'],
  1: ['enable', '生效'],
})

// 规则运算符
export const RULE_OPER = generateDicObj({
  1: ['qie', '且'],
  2: ['huo', '或'],
})

// 审批状态 0未审批 1通过 2驳回
export const APPROVE_STATUS = generateDicObj({
  0: ['wsp', '未审批'],
  1: ['tg', '通过'],
  2: ['bh', '驳回'],
})

// 规则运算符
export const CONNECT_LIST = generateDicObj({
  0: ['dydy', '大于等于'],
  1: ['dy', '大于'],
  2: ['xydy', '小于等于'],
  3: ['xy', '小于'],
  4: ['dy', '等于'],
})

// 以下没有用到、、、、、、、、、、、、、、、、

// 投诉流程状态标识
export const TS_PARTAKE_STATUS = generateDicObj({
  21: ['uncheck', '待受理'],
  22: ['accepted', '转效能办'],
  23: ['reject', '不予受理'],
  // 25: ['pdReject', '派单不予受理'],
  30: ['pdCancel', '派单撤回'],
  41: ['xbpd', '派单'],
  42: ['pd', '派单'],
  51: ['replied', '已答复'],
  81: ['zyjResove', '资源局同意'],
  82: ['zyjReject', '资源局不同意'],
})
export const TS_PARTAKE_TYPE = generateDicObj({
  21: ['uncheck', '待受理'],
  22: ['accepted', '转效能办'],
  23: ['reject', '不予受理'],
  // 25: ['pdReject', '派单不予受理'],
  30: ['pdCancel', '派单撤回'],
  41: ['xbpd', '派单'],
  42: ['pd', '派单'],
  51: ['replied', '已答复'],
  81: ['zyjResove', '资源局同意'],
  82: ['zyjReject', '资源局不同意'],
})
